package example.controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.conversion.Asset;
import com.google.appengine.api.conversion.Conversion;
import com.google.appengine.api.conversion.ConversionResult;
import com.google.appengine.api.conversion.ConversionService;
import com.google.appengine.api.conversion.ConversionServiceFactory;
import com.google.appengine.api.conversion.Document;
import com.google.appengine.api.search.query.QueryParser.restrict_return;

import example.helper.CrawlerHelper;

public class ConversionExample extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws IOException {
		convertPNG(resp);
	}
	
	public static void convertPNG(HttpServletResponse resp) {
		try {
			String content = CrawlerHelper.getContentFromUrl("http://google.com.vn");
			Asset asset = new Asset("text/html", content.getBytes(), "google.html");
			Document document = new Document(asset);
			Conversion conversion = new Conversion(document, "image/png");
			
			ConversionService service = ConversionServiceFactory.getConversionService();
			ConversionResult result = service.convert(conversion);
			if (result.success()) {
				for (Asset assetR: result.getOutputDoc().getAssets()) {
					resp.setContentType("image/png");
					resp.getOutputStream().write(assetR.getData());
					System.out.println(assetR.getData());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
}
